<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>54数组扁平化、去重与排序</title>
</head>
<body>
<script>
  let arr = [40,9,20,4,[1, 2, 3, 4], [2, 3, 4, [1,2,3,[1,2,3]],5], [1, 2, 3, 4], [3, 4, 5, 9]]



  Array.prototype.flatten = function () {
    let _arr = this,
      toStr = {}.toString

    if (toStr.call(_arr) !== "[object Array]"){
      throw new Error('必须是数组')
    }

    let fArr = []
    _arr.forEach(function (item) {
      if (toStr.call(item) === "[object Array]"){
        fArr = fArr.concat(item.flatten())
      }else {
        fArr.push(item)
      }
    })

    return fArr = Array.from(new Set(fArr)).sort((a,b)=>{
      return a - b
    });
  }

  console.log(arr.flatten());
</script>
</body>
</html>